<?php
namespace app\model;



use app\lib\WxoffiAccount;
use think\Session;

class Fans {

    static function getRealUid()
    {
        global $_G;
        return $_G['member']['uid'];
    }
    static function get($openid){
        global $_G, $_P;
        if(empty($openid)){
            $openid = $_G['openid'];
        }
        $params = array();
        $params[':openid'] = $openid;
        $fans = db_find("SELECT * FROM ".tablename(t_fans)."
			WHERE openid = :openid",$params);
        return $fans;
    }

    static function getuid_old($uid,$themeid){
        if (empty($uid)) {
            return true;
        } 
        $fromuid_list = db_find("SELECT * FROM ".tablename(t_fans)." WHERE themeid = " . $themeid . " and uid_old = " . $uid );
        $r = false;
        if (!empty($fromuid_list)) {
            if ($fromuid_list['uid'] == $fromuid_list['uid_old']){
                $r = true;
            }
        }
        return $r;
    }
    static function getworkerid_old($uid,$themeid){
        if (empty($uid)) {
            return true;
        } 
        $fromuid_list = db_find("SELECT * FROM ".tablename(t_worker)." WHERE themeid = " . $themeid . " and uid_old = " . $uid );
        $r = false;
        if (!empty($fromuid_list)) {
            if ($fromuid_list['uid'] == $fromuid_list['uid_old']){
                $r = true;
            }
        }
        return $r;
    }


    static function getByTheme($themeid){
        global $_G, $_P;
        $params = array();
        $params[':themeid'] = $themeid;
        $params[':openid'] = $_G['openid'];
        $fans = db_find("SELECT * FROM ".tablename(t_fans)."
			WHERE themeid= :themeid and openid = :openid",$params);
        return $fans;
    }

    static function getById($uid,$themeid){
        global $_G, $_P;
        $params = array();
        $params[':themeid'] = $themeid;
        $params[':uid'] = $uid;
        $fans = db_find("SELECT * FROM ".tablename(t_fans)."
            WHERE themeid= :themeid and uid = :uid",$params);
        return $fans;
    }

	
	static function fansAuth($openid = '') {
		global $_G, $_P;
		if (empty($openid)) {
			$openid = $_G['openid'];
		}
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (!DEVELOPMENT && (strpos($user_agent, 'MicroMessenger') === false || empty($_G['openid']))) {
            die("
		        
		               
		           ");
            return;
        }
        //load_model('mc');
        if(!empty($_G['openid'])){
            $fans = mc_fansinfo($_G['openid']);
            if(empty($fans)) {
                $fans = mc_oauth_userinfo();
            }
        }else{
            $fans = mc_oauth_userinfo();
        }
        if(empty($_G['member']['uid'])){
            $uid = mc_openid2uid($_G['openid']);
            if($uid > 0){
                $_G['member']['uid'] = $uid;
            }else{
                $member_info = mc_init_fans_info($_G['openid'], true);
                $_G['member']['uid'] = $member_info['uid'];
            }
        }
        return $fans;
	}



}
